package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.CustomReport;
import com.example.mapp.CustomReportMapp;
import com.example.service.CustomReportService;
import com.example.util.DateUtils;
import com.example.util.ResultUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 自定义查询界面
 */
@Service
public class CustomReportServiceImpl implements CustomReportService {

	private final static Logger logger = (Logger) LoggerFactory.getLogger(CustomReportServiceImpl.class);
	// 自定义维护表
	@Autowired
	private CustomReportMapp customReportMapp;
	/**
	 * 查询某一段时间内得维保信息
	 * @return
	 */
	@Override
	public Map<String, Object> findList(CustomReport customReport){
		String message = "查询成功";
		List<CustomReport> list = null;
		try {
			// 通过id查询SQL
			list =  customReportMapp.findList(customReport);
		}catch (Exception e){
			logger.error(e.getMessage());
			message = "查询失败";
			return ResultUtil.returnError(message + e.getMessage(), null);
		}
		return ResultUtil.returnSuccess(message, list);
	}

	/**
	 * 新增数据
	 * @param customReport
	 * @return
	 */
	@Override
	public Map<String, Object> insert(CustomReport customReport) {
		String message = "添加成功";
		Map<String, Object> resultMap = new HashMap<>();
		try {
			customReport.setCreateDate(DateUtils.formatDate(new Date(),"yyyy-MM-dd"));
			customReport.setDelFlag("1");
			customReportMapp.insert(customReport);
		}catch (Exception e){
			logger.error(e.getMessage());
			message = "添加失败";
			return ResultUtil.returnError(message + e.getMessage(), null);
		}
		return ResultUtil.returnSuccess(message, resultMap);
	}

	/**
	 * 修改数据
	 * @param customReport
	 * @return
	 */
	@Override
	public Map<String, Object> update(CustomReport customReport) {
		String message = "修改成功";
		Map<String, Object> resultMap = new HashMap<>();
		try {
			customReport.setUpdateDate(DateUtils.formatDate(new Date(),"yyyy-MM-dd"));
			customReportMapp.update(customReport);
		}catch (Exception e){
			logger.error(e.getMessage());
			message = "修改失败";
			return ResultUtil.returnError(message + e.getMessage(), null);
		}
		return ResultUtil.returnSuccess(message, resultMap);
	}

	/**
	 * 批量删除
	 * @param
	 */
	@Override
	public Map<String,Object> delete(List<String> ids)  {
		String message = "删除成功";
		Map<String, Object> resultMap = new HashMap<>();
		try {
			for(int i=0;i<ids.size();i++){
				customReportMapp.delete(ids.get(i));
			}
		}catch (Exception e){
			logger.error(e.getMessage());
			message = "删除失败";
			return ResultUtil.returnError(message + e.getMessage(), null);
		}
		return ResultUtil.returnSuccess(message, resultMap);
	}
}
